package com.onpositive.collections;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import com.onpositive.notes.model.ITemplateModel;
import com.onpositive.notes.services.ICollectionRegistry;

public class CollectionsContainer implements IObjectCollection {

	private static final long serialVersionUID = -7197267476038492788L;

	public CollectionsContainer(String name, ICollectionRegistry registry) {
		super();
		this.name = name ;
		this.registry = registry ;
	}
	
	private String name ;
	
	ICollectionRegistry registry ;
	
	private int id;
	
	ArrayList<String> children = new ArrayList<String>();

	
		
	@Override
	public String getName() {
		return name ;
	}

	@Override
	public List<ICollectionMember<?>> getMembersByFirstWord(String keyWord) {
		
		ArrayList<ICollectionMember<?>> result = new ArrayList<ICollectionMember<?>>() ;
		for( String colName : children ){
			IObjectCollection col = registry.getCollectionByName(colName) ;
			List<ICollectionMember<?>> members = col.getMembersByFirstWord(keyWord);
			result.addAll(members) ;
		}		
		return result ;
	}

	@Override
	public ICollectionMember<?> getMemberByName(String name) {
		for( String colName : children ){
			IObjectCollection col = registry.getCollectionByName(colName) ;
			ICollectionMember<?> member = col.getMemberByName(name) ;
			if( member != null )
				return member ;
		}		
		return null ;
	}

	@Override
	public List<ICollectionMember<?>> getAllMembers() {
		ArrayList<ICollectionMember<?>> result = new ArrayList<ICollectionMember<?>>() ;
		for( String colName : children ){
			IObjectCollection col = registry.getCollectionByName(colName) ;
			List<ICollectionMember<?>> members = col.getAllMembers() ;
			for( ICollectionMember<?> member : members )
				result.add( new MemberWrapper(member, this) ) ;
		}		
		return result ;
	}

	@Override
	public List<ITemplateModel<?>> getAffectingTemplates() {
		ArrayList<ITemplateModel<?>> result = new ArrayList<ITemplateModel<?>>() ;
		for( String colName : children ){
			IObjectCollection col = registry.getCollectionByName(colName) ;
			List<ITemplateModel<?>> templates = col.getAffectingTemplates() ;
			result.addAll(templates) ;
		}		
		return result ;
	}

	@Override
	public List<ICollectionDelta<?>> getCompleteHistory() {
		
		ArrayList<ICollectionDelta<?>> result = new ArrayList<ICollectionDelta<?>>() ;
		for( String colName : children ){
			IObjectCollection col = registry.getCollectionByName(colName) ;
			List<ICollectionDelta<?>> history = col.getCompleteHistory();
			result.addAll(history) ;
		}
		Comparator<ICollectionDelta<?>> dateComparator = new Comparator<ICollectionDelta<?>>() {
			
			@Override
			public int compare(ICollectionDelta<?> arg0, ICollectionDelta<?> arg1) {
				Date date0 = arg0.getDate();
				Date date1 = arg1.getDate();
				
				if(date0==null){
					if(date1 == null)
						return 0 ;
					else
						return -1 ;
				}
				else if (date1 == null)
					return 1 ;
				
				return date0.compareTo(date1);
			}
		} ;
		
		Collections.sort( result, dateComparator ) ;
		return result ;
	}

	@Override
	public List<ICollectionDelta<?>> getMemberHistory( ICollectionMember<?> member) {
		return member.getCollection().getMemberHistory(member) ;
	}

	@Override
	public ICollectionMember<?> getMemberById(int id) {
		return null;
	}

	@Override
	public int registerDelta(ICollectionDelta<?> delta) {
		return 0;
	}

	@Override
	public int getId() {
		return id ;
	}

	@Override
	public void setId(int id) {
		this.id = id ;
	}

	@Override
	public int addNewMember(ICollectionMember<?> member) {
		return 0;
	}

	@Override
	public void removeDelta(int deltaId) {
	}

	@Override
	public Object getCurrentValue(ICollectionMember<?> member) {		
		return member.getCollection().getCurrentValue(member) ;
	}
	
	public void addChild( String name ){
		children.add( name ) ;		
	}
	
	public static class MemberWrapper implements ICollectionMember<Object>{
		
		private static final long serialVersionUID = 5940949650044180469L;

		public MemberWrapper(ICollectionMember<?> member, CollectionsContainer container) {
			super();
			this.member = member;
			this.container = container ;
		}

		private ICollectionMember<?> member ;
		private CollectionsContainer container ;

		@Override
		public String getName() {
			return member.getName() ;
		}

		@Override
		public int getId() {
			return -1 ;
		}

		@Override
		public void setId(int id) {}

		@Override
		public IObjectCollection getCollection() {
			return container;
		}

		@Override
		public void setCollectionId(int collectionId) {}

		@Override
		public int getCollectionId() {
			return container.getId() ;
		}

		@Override
		public Object getCurrentValue() {
			return member.getCurrentValue() ;
		}

		public ICollectionMember<?> getMember() {
			return member;
		}		
	}

}
